(function(){

String.prototype.replaceAll = function(arg1, arg2){
	var i = this.indexOf(arg1);
	var result = this;
	while( i >= 0 ){
		result = result.replace(arg1, arg2);
		i = result.indexOf(arg1);
	}
	return result;
}
_isFn = function(member){
	return typeof member == "function";
};
_isNumber = function(value){
	return !isNaN(value-0);
};

iterate = function(collection, callback){
	if( !_isFn(callback) ){
		console.log("Hmm");
	}
	if( collection.length ){
		for(var i=0; i < collection.length; i++){
			if(collection[i] && callback(collection[i], i) )
				return true;
		}
	}
	for(var member in collection){
		if( _isFn( collection[member]) || (_isNumber(member)&&collection.length) )
			continue;
		if(collection[member] && callback(collection[member], member) )
			return true;
	}
};
Array.prototype.indexOf = function(obj){
	var result = -1;
	iterate(this, function(item, index){
		result = index;
	});
	return result;
};
//Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to){
	var rest = this.slice((to||from)+1||this.length);
	this.length = from < 0 ? this.length + from : from;
	var result= this.push.apply(this,rest);
	return result || [];
};

var __scripts = {};
var __readyToLoad = [];
var __dependenciesLoaded = false;
var __scriptPath = 'js/'
try{
	__scriptPath = scriptPath || 'js/'
}catch(ignore){}

toNumber = function(n){
	if( n ){
		n = n.replaceAll('px')
		return parseInt(n);
	}
	return 0;
}

include = function(names, callback){
	var sc = new SyncCallback(names, callback);
	var scripts = [];
	for(var i=0; i < names.length; i++){
		var name = names[i];
		var dependency = __scripts[name];
		if(!dependency){
			dependency = {src:__scriptPath + name.replaceAll('.', '/') + '.js', callbacks:[], name:name}
			__scripts[name] = dependency;
		}
		if( !dependency.loaded ){
			if( !dependency.pending ){
				dependency.pending = true;
				scripts.push(dependency);
			}
			dependency.callbacks.push(sc);
		}else if(callback){
			sc.done(name);
		}
	}
	for(var i=0; i < scripts.length;i++){
		var script = scripts[i];
		//console.log("Import " + script.name);
		__readyToLoad.push(script);
	}
	if(__dependenciesLoaded)
		loadDependencies();
}
declare = function(name){
	var dependency = __scripts[name];
	if(!dependency){
		dependency = {name:name, loaded:false}
		__scripts[name] = dependency;
	}
	if( dependency.loaded ){
		throw 'Dependency already loaded: ' + name;
	}
	//console.log(name + " - Ready");
	
	dependency.loaded = true;
	if( dependency.pending ){
		var sync = dependency.callbacks.pop();
		while(sync){
			sync.done(name);
			sync = dependency.callbacks.pop();
		}
	}	
}
loadDependencies = function(){
	__dependenciesLoaded = true;
	
	var script = __readyToLoad.shift();
	while(script){
		if(script.loaded){
			script = __readyToLoad.shift();
			continue;
		}
		
		process(script);
		
		script = __readyToLoad.shift();
	}
}
var process = function(script){
	if(script.loaded || script.created){
		return;
	}
	//console.log("Creating tag: " + script.src);
	var tag = document.createElement('script');
	var scope = script;
	$(tag).attr('type', 'text/javascript');
	$(tag).attr('src', script.src);
	document.getElementsByTagName('head')[0].appendChild(tag);
	script.created = true;
}
var __callbacksRunning = false;
var __callbacks = [];
var __doCallbacks = function(){
	if(__callbacksRunning) return;
	__callbacksRunning = true;
	var next = __callbacks.pop();
	while(next){
		try{
			next();
		}catch(e){
			if( !/Dependency already.*/.test(e) )
				console.error(e);
		}
		next = __callbacks.pop();
	}
	__callbacksRunning = false;
}
var SyncCallback = function(names, callback){
	this.count = names.length;
	this.callback = callback;
	this.done = function(name){
		for(var i=0; i < names.length; i++)
			if(names[i] == name) names[i] = null;
		//console.log("Waiting on : " + names);
		this.count--;
		if( !this.count && this.callback ){
			__callbacks.push(callback);
			__doCallbacks();
		}
	};
	if(!this.count && callback){
		__callbacks.push(callback);
		__doCallbacks();
	}
};

})();
$(function(){
	__dependenciesLoaded = true;
	loadDependencies();
});